package status;

import java.io.File;
import java.util.HashMap;

public class FileItemStatus {

	public final int NUMBER_OF_RESULTS = 10;
	private int numberOfDownloads;
	private long averageServiceTime;
	private long[] listOfDownloadTimes;
	private long quickestServiceTime;
	private long slowestServiceTime;
	private int counter;
	private HashMap<String,Integer> mapOfUserDownloads;
		
	public FileItemStatus() {
		this.numberOfDownloads = 0;
		this.averageServiceTime = 0;
		this.quickestServiceTime = 0;
		this.slowestServiceTime = 0;
		this.listOfDownloadTimes = new long[NUMBER_OF_RESULTS];
		this.counter = 0;
	}
	
	public FileItemStatus(int numberOfDownloads, long averageServiceTime,
			long quickestServiceTime, long slowestServiceTime) {
		this.numberOfDownloads = numberOfDownloads;
		this.averageServiceTime = averageServiceTime;
		this.quickestServiceTime = quickestServiceTime;
		this.slowestServiceTime = slowestServiceTime;
		this.listOfDownloadTimes = new long[NUMBER_OF_RESULTS];
		this.counter = 0;
	}

	public void incrementDownloadCount(){
		this.numberOfDownloads++;
	}
	public synchronized void updateServiceTime(long serviceTime){
		//Zero seconds will always be quicker, so first time round, the first download is the best.
		if(quickestServiceTime == 0){
			quickestServiceTime = serviceTime;
		}
		if(quickestServiceTime != 0 && serviceTime < quickestServiceTime){
			quickestServiceTime = serviceTime;
		}
		if(serviceTime > slowestServiceTime){
			slowestServiceTime = serviceTime;
		}
		long total = 0;
		for(int i = 0; i < NUMBER_OF_RESULTS; i++){
			if(listOfDownloadTimes[i] == 0){
				listOfDownloadTimes[i] = serviceTime;
			}
			total += listOfDownloadTimes[i] ;
		}
		this.averageServiceTime = total / NUMBER_OF_RESULTS;
		//Reset Counter
		if(counter == (NUMBER_OF_RESULTS-1)){
			counter = 0;
		}
		listOfDownloadTimes[counter] = serviceTime;
		counter++;		
	}
	public void updateClientDownload(String clientId){
		if(mapOfUserDownloads.containsKey(clientId)){
			Integer intValue = mapOfUserDownloads.get(clientId);
			mapOfUserDownloads.put(clientId, intValue++);
		}else{
			mapOfUserDownloads.put(clientId, Integer.parseInt("1")) ;
		}
	}

	public int getNumberOfDownloads() {
		return numberOfDownloads;
	}

	public long getAverageServiceTime() {
		return averageServiceTime;
	}

	public long getQuickestServiceTime() {
		return quickestServiceTime;
	}

	public long getSlowestServiceTime() {
		return slowestServiceTime;
	}
	
	
}
